

layui.define(['jquery', 'table', 'permission', 'form', 'context'], function (exports) {
	"use strict";
	/**
	 * 常用封装类
	 * */
	var MOD_NAME = 'common',
		$ = layui.jquery,
		table = layui.table,
		context = layui.context,
		form = layui.form;

	let basePath = context.get("base-path");

	var common = new function () {
		//加载下拉框
		this.loadSelect = function (_opntions) {
			var opntions = {
				divId: _opntions.divId,
				url: _opntions.url,
				type: _opntions.type || "post",
				data: _opntions.data,
				async: _opntions.async || false,
				value: _opntions.value || "",//选中值
				keyField: _opntions.keyField || "key",
				valueField: _opntions.valueField || "value",
				defaultItem: _opntions.defaultItem || null,//缺省值
			}
			$.ajax({
				url: opntions.url,
				data: opntions.data,
				type: opntions.type,
				async: opntions.async,
				success: function (result) {
					$(opntions.divId).empty();
					if (opntions.defaultItem != null && opntions.defaultItem != "") {
						$(opntions.divId).append("<option value=\"\">" + opntions.defaultItem + "</option>");
					}
					$.each(result.data, function (index, value) {
						if (opntions.value == value[opntions.keyField]) {
							$(opntions.divId).append("<option value=\"" + value[opntions.keyField] + "\" selected>" + value[opntions.valueField] + "</option>")
						} else {
							$(opntions.divId).append("<option value=\"" + value[opntions.keyField] + "\">" + value[opntions.valueField] + "</option>")
						}
					})
					form.render('select');
				}
			})
		}
		//加载xm多选下拉框
		this.loadXmSelect = function (_opntions) {
			var opntions = {
				divId: _opntions.divId,
				name: _opntions.name,//表单取值时对应的name
				url: _opntions.url,
				type: _opntions.type || "post",
				data: _opntions.data,
				filterable: _opntions.filterable || true,
				async: _opntions.async || false,
				value: _opntions.value || [],//选中值
				keyField: _opntions.keyField || "key",
				valueField: _opntions.valueField || "value",
			}
			$.ajax({
				url: opntions.url,
				data: opntions.data,
				type: opntions.type,
				async: opntions.async,
				success: function (result) {
					$(opntions.divId).empty()
					var dataArr = [];
					$.each(result.data, function (index, value) {
						var data = {
							value: value[opntions.keyField],
							name: value[opntions.valueField],
						};
						if (opntions.value.indexOf(value[opntions.keyField]) > -1) {
							data.selected = true;
						}
						dataArr.push(data)
					})
					xmSelect.render({
						el: opntions.divId,
						name: opntions.name,
						filterable: opntions.filterable,
						theme: {
							color: '#36b368',
						},
						data: dataArr,
					})
				}
			})
		}
		//加载xm多选下拉框-树
		this.loadXmSelectTree = function (_opntions) {
			var opntions = {
				divId: _opntions.divId,
				name: _opntions.name,//表单取值时对应的name
				url: _opntions.url,
				type: _opntions.type || "post",
				data: _opntions.data,
				filterable: _opntions.filterable || true,
				async: _opntions.async || false,
				value: _opntions.value || [],//选中值
				tree: {
					show: true,
					strict: false,
					expandedKeys: [-1],
				},
				radio: _opntions.radio || true,
				clickClose: _opntions.clickClose || true,
			}
			$.ajax({
				url: opntions.url,
				data: opntions.data,
				type: opntions.type,
				async: opntions.async,
				success: function (result) {
					$(opntions.divId).empty()
					xmSelect.render({
						el: opntions.divId,
						name: opntions.name,
						radio: opntions.radio,
						clickClose: opntions.clickClose,
						model: { label: { type: 'text' } },
						filterable: opntions.filterable,
						theme: {
							color: '#36b368',
						},
						tree: {
							show: true,
							strict: false,
							expandedKeys: [-1],
						},
						data: result.data,
					})
				}
			})
		}
		//加载单选
		this.loadRadio = function (_opntions) {
			var opntions = {
				divId: _opntions.divId,
				name: _opntions.name,
				url: _opntions.url,
				type: _opntions.type || "post",
				data: _opntions.data,
				async: _opntions.async || false,
				value: _opntions.value || "",//选中值
				valueField: _opntions.valueField || "value",
				textField: _opntions.textField || "text",
			}
			$.ajax({
				url: opntions.url,
				data: opntions.data,
				type: opntions.type,
				async: opntions.async,
				success: function (result) {
					$(opntions.divId).empty();
					$.each(result.data, function (index, item) {
						if (opntions.value == item.value) {
							$(opntions.divId).append("<input type=\"radio\" name=\"" + opntions.name + "\" value=\"" + item.value + "\" + title=\"" + item.text + "\" checked>")
						} else {
							$(opntions.divId).append("<input type=\"radio\" name=\"" + opntions.name + "\" value=\"" + item.value + "\" + title=\"" + item.text + "\">")
						}
					})
					form.render();
				}
			})
		}
		//获取当前url参数
		this.getQueryString = function (name) {
			var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
			var r = window.location.search.substr(1).match(reg);
			if (r != null) {
				return unescape(r[2]);
			}
			return null;
		}
		//构建巨益签名
		this.buildJeomsSign = function () {
			var sign = "";
			$.ajax({//构建巨益签名
				url: basePath + '/api/workorder/common/buildjeomssign',
				async: false,
				success: function (result) {
					if (result.succeeded) {
						sign = result.data;
					} else {
						popup.failure(JSON.stringify(result.errors));
					}
				}
			})
			return sign;
		}

		//判断2个数组是否相等
		this.ArrayIsEqual = function (arr1, arr2) {
			if (arr1.length != arr2.length) {
				return false;
			} else {//长度相同
				for (let i in arr1) {//循环遍历对比每个位置的元素
					if (arr1[i] != arr2[i]) {//只要出现一次不相等，那么2个数组就不相等
						return false;
					}
				}//for循环完成，没有出现不相等的情况，那么2个数组相等
				return true;
			}
		}


		this.serializeJson = function (formId) {
			var serializeObj = {};
			var $form = $(formId);
			var array = $form.serializeArray();
			$(array).each(
				function () {
					if (serializeObj[this.name]) {
						if ($.isArray(serializeObj[this.name])) {
							serializeObj[this.name].push(this.value);
						} else {
							serializeObj[this.name] = [
								serializeObj[this.name], this.value];
						}
					} else {
						serializeObj[this.name] = this.value;
					}
				});
			return serializeObj;
		}


		// /**
		//  * 获取当前表格选中字段
		//  * @param obj 表格回调参数
		//  * @param field 要获取的字段
		//  * */
		// this.checkField = function(obj, field) {
		// 	let data = table.checkStatus(obj.config.id).data;
		// 	if (data.length === 0) {
		// 		return "";
		// 	}
		// 	let ids = "";
		// 	for (let i = 0; i < data.length; i++) {
		// 		ids += data[i][field] + ",";
		// 	}
		// 	ids = ids.substr(0, ids.length - 1);
		// 	return ids;
		// }

		// /**
		//  * 当前是否为与移动端
		//  * */
		// this.isModile = function(){
		// 	if ($(window).width() <= 768) {
		// 		return true;
		// 	}
		// 	return false;
		// }


		// /**
		//  * 提交 json 数据
		//  * @param data 提交数据
		//  * @param href 提交接口
		//  * @param table 刷新父级表
		//  * 
		//  * */
		// this.submit = function(data,href,table,callback){
		// 	$.ajax({
		// 	    url:href,
		// 	    data:JSON.stringify(data),
		// 	    dataType:'json',
		// 	    contentType:'application/json',
		// 	    type:'post',
		// 	    success:callback !=null?callback(result):function(result){
		// 	        if(result.success){
		// 	            layer.msg(result.msg,{icon:1,time:1000},function(){
		// 	                parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
		// 	                parent.layui.table.reload(table);
		// 	            });
		// 	        }else{
		// 	            layer.msg(result.msg,{icon:2,time:1000});
		// 	        }
		// 	    }
		// 	})
		// }
	}
	exports(MOD_NAME, common);
});
